<template>
    <el-card class="page-container">
      <template #header>
        <div class="header">
          <span>用户密码修改</span>
        </div>
      </template>
      <div class="form-container">
        <el-form :model="passwordForm" label-width="100px" :rules="rules" ref="passwordFormRef">
          <el-form-item label="旧密码" prop="oldPassword">
            <el-input type="password" v-model="passwordForm.oldPassword" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword">
            <el-input type="password" v-model="passwordForm.newPassword" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="confirmPassword">
            <el-input type="password" v-model="passwordForm.confirmPassword" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </template>
  
  <script setup>
  import { ref } from 'vue';
  import { ElMessage } from 'element-plus';
  import { updatePasswordService } from '@/api/userApi/user.js';
  
  const passwordForm = ref({
    oldPassword: '',
    newPassword: '',
    confirmPassword: ''
  });
  
  const rules = {
    oldPassword: [
      { required: true, message: '请输入旧密码', trigger: 'blur' }
    ],
    newPassword: [
      { required: true, message: '请输入新密码', trigger: 'blur' },
      { min: 4, message: '密码长度不能少于4位', trigger: 'blur' }
    ],
    confirmPassword: [
      { required: true, message: '请确认新密码', trigger: 'blur' },
      { validator: (rule, value, callback) => {
        if (value !== passwordForm.value.newPassword) {
          callback(new Error('两次输入的新密码不一致'));
        } else {
          callback();
        }
      }, trigger: 'blur' }
    ]
  };
  
  const passwordFormRef = ref(null);
  
  const submitForm = async () => {
    passwordFormRef.value.validate(async (valid) => {
      if (valid) {
        const result = await updatePasswordService(passwordForm.value);
        ElMessage.success('修改成功');
      }
    });
  };
  
  const resetForm = () => {
    passwordFormRef.value.resetFields();
  };
  </script>
  
  <style lang="scss" scoped>
  .page-container {
    min-height: 100%;
    box-sizing: border-box;
  
    .header {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
  
    .form-container {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100%;
    }
  
    .el-form {
      width: 400px;
    }
  }
  </style>